/*
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the FxStruts MailReader Application.
 *
 * The Initial Developer of the Original Code is
 * Anirudh Sasikumar (http://anirudhs.chaosnet.org/).
 * Portions created by the Initial Developer are Copyright (C) 2008
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
*/

package com.adobe.mailreader.command
{
    import com.adobe.cairngorm.commands.ICommand;
    import com.adobe.cairngorm.control.CairngormEvent;
    import com.adobe.mailreader.business.SubscriptionDelegate;
    import com.adobe.mailreader.model.MailModelLocator;
    import com.adobe.mailreader.vo.UserVO;
    
    import mx.collections.ArrayCollection;
    import mx.controls.Alert;
    import mx.rpc.IResponder;
    
    /* Get user details including subscription */
    public class SubscriptionCommand implements ICommand, IResponder
    {
        public function SubscriptionCommand()
        {
            
        }
        
        public function execute( event:CairngormEvent ):void
        {
            var subDelegate:SubscriptionDelegate = new SubscriptionDelegate(this);      	            	 
            subDelegate.getSubscription(); 	              
        }
        
        /* Read VO into our model */
        private function readFromUserVO(uservo:UserVO):void
        {
            var model : MailModelLocator = MailModelLocator.getInstance();
            model.fromAddress = uservo.fromAddress;
            model.fullName = uservo.fullName;
            model.replyToAddress = uservo.replyToAddress;
            model.userName = uservo.username;                	
            model.mailSubscriptions = new ArrayCollection(uservo.subscriptions);
        }
        
        public function result( event : Object ) : void
        {				
            trace("Got result " + event.result);
            var model : MailModelLocator = MailModelLocator.getInstance();
            model.transactionToken = "";
            var uservo:UserVO;                
            if ( event.result is Object && event.result.hasOwnProperty("result") && 
                 (event.result.hasOwnProperty("org.apache.struts.taglib.html.TOKEN") || 
                  event.result.hasOwnProperty("errors"))  )
            {
                /* if a vo is returned, read it into our model */
                if ( event.result.result is UserVO )
                {
                    uservo = event.result.result as UserVO;
                    readFromUserVO(uservo);
                }
                /* check for token and save it */
                if ( event.result.hasOwnProperty("org.apache.struts.taglib.html.TOKEN") )
                {
                    model.transactionToken = event.result["org.apache.struts.taglib.html.TOKEN"];
                    model.notifyView(MailModelLocator.MODEL_GOT_TOKEN);
                }                	
            }
            else if ( event.result is UserVO )
            {
                uservo = event.result as UserVO;
                readFromUserVO(uservo);
            }
            else
            {
                trace ("Warning: got an anon obj rather than UserVO for SubscriptionCommand");
            }	
            
        }
        
        public function fault( event : Object ) : void
        {
            trace("Got fault" + event);
            Alert.show("Subscription command error: " + event);
        }
        
    }
}